//
//  BDPostDetailCell.m
//  BadBoy
//
//  Created by wen on 2017/9/6.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDPostDetailCell.h"
#import "BDPostCell.h"
#import "BDGeneralCell.h"


@interface BDPostDetailCell ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) BDPostDetailModel *detailModel;

@end

@implementation BDPostDetailCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kBDScreenWidth, 0) style:UITableViewStyleGrouped];
        _tableView.sectionHeaderHeight = 0;
        _tableView.sectionFooterHeight = 0;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kBDScreenWidth, 0.1)];
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.scrollEnabled = NO;
        [_tableView registerClass:[BDPostCell class] forCellReuseIdentifier:@"topCell"];
        [self.contentView addSubview:_tableView];
    }
    return self;
}

- (void)configeWithModel:(id)model{
    _detailModel = model;
    _tableView.height = _detailModel.tableViewHeight;
    [_tableView reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    BOOL haveShopInfo = _detailModel.shopInfo.count != 0;
    return haveShopInfo + 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }
    return _detailModel.shopInfo.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return _detailModel.layout.detailHeight;
    }
    return tableViewcellHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section != 0) {
        return tableViewSectionHeight;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        NSString *cellID = @"topCell";
        BDPostCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
//        cell.delegate = self.viewController;
        [cell setLayout:_detailModel.layout needCoverText:YES];
        return cell;
    }
    BDGeneralModel *generalModel = _detailModel.shopInfo[indexPath.row];
    BDGeneralCell *cell = [BDGeneralCell cellForTableView:tableView model:generalModel indexPath:indexPath identifier:@"postDetailCell"];
    cell.subviewBlock = ^(BDGeneralCell *generalCell) {
        generalCell.imageView.size = CGSizeMake(20, 20);
        generalCell.imageView.left = 10;
        generalCell.imageView.centerY = generalCell.height / 2.0;
        
        generalCell.textLabel.font = [UIFont systemFontOfSize:14];
        generalCell.textLabel.textColor = [UIColor colorWithHexString:@"#4c4c4c"];
        [generalCell.textLabel sizeToFit];
        generalCell.textLabel.left = generalCell.imageView.right + 10;
        generalCell.textLabel.centerY = generalCell.height / 2.0;
    };
    return cell;
}

@end
